package com.server.server_a10.management.controller;

import com.server.server_a10.entity.Inform;
import com.server.server_a10.management.entity.EquipmentInf;
import com.server.server_a10.management.entity.ManageInf;
import com.server.server_a10.mapper.GardenMapper;
import com.server.server_a10.mapper.HeaterMapper;
import com.server.server_a10.mapper.LightMapper;
import com.server.server_a10.mapper.SmogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class manageEquipmentHandle {
    @Autowired
    GardenMapper gardenMapper;
    @Autowired
    SmogMapper smogMapper;
    @Autowired
    LightMapper lightMapper;
    @Autowired
    HeaterMapper heaterMapper;

    /*
    ============================================================================
                                  garden
    ============================================================================
     */

    @GetMapping(value = "getGardenLimit")
    public ManageInf getGardenLimit(@RequestParam("limit") int limit,
                                    @RequestParam("page") int page,
                                    @RequestParam("name") String name,
                                    @RequestParam("room_id") String room_id,
                                    @RequestParam("home_id") String home_id) {
        ManageInf inform = new ManageInf();

        int index = (page - 1) * limit;

        List<EquipmentInf> gardenInfs;

        gardenInfs = gardenMapper.getGardenLimit(name, room_id, home_id, index, limit);
        int count = gardenMapper.getGardenLimitCount(name, room_id, home_id);

        inform.setCount(count);
        if (gardenInfs.isEmpty()) {
            inform.isNull("似乎没有任何数据");
        } else {
            inform.success("信息加载成功", gardenInfs);
        }

        return inform;
    }

//    @PostMapping(value = "insGarden")
//    public ManageInf getGardenLimit(@RequestBody Map<String,Object> request) {
//        Inform inform = new Inform();
//
//
//        int isOk =
//
//        if (gardenInfs.isEmpty()) {
//            inform.isNull("似乎没有任何数据");
//        } else {
//            inform.success("信息加载成功", gardenInfs);
//        }
//
//        return inform;
//    }

    /*
   ============================================================================
                                 smog
   ============================================================================
    */
    @GetMapping(value = "getSmogLimit")
    public ManageInf getSmogLimit(@RequestParam("limit") int limit,
                                  @RequestParam("page") int page,
                                  @RequestParam("name") String name,
                                  @RequestParam("room_id") String room_id,
                                  @RequestParam("home_id") String home_id) {
        ManageInf inform = new ManageInf();

        int index = (page - 1) * limit;

        List<EquipmentInf> smogInfs;

        smogInfs = smogMapper.getSmogLimit(name, room_id, home_id, index, limit);
        int count = smogMapper.getSmogLimitCount(name, room_id, home_id);

        inform.setCount(count);
        if (smogInfs.isEmpty()) {
            inform.isNull("似乎没有任何数据");
        } else {
            inform.success("信息加载成功", smogInfs);
        }

        return inform;
    }


    /*
   ============================================================================
                                 light
   ============================================================================
    */
    @GetMapping(value = "getLightLimit")
    public ManageInf getLightLimit(@RequestParam("limit") int limit,
                                   @RequestParam("page") int page,
                                   @RequestParam("name") String name,
                                   @RequestParam("room_id") String room_id,
                                   @RequestParam("home_id") String home_id) {
        ManageInf inform = new ManageInf();

        int index = (page - 1) * limit;

        List<EquipmentInf> smogInfs;

        smogInfs = lightMapper.getLightLimit(name, room_id, home_id, index, limit);
        int count = lightMapper.getLightLimitCount(name, room_id, home_id);

        inform.setCount(count);
        if (smogInfs.isEmpty()) {
            inform.isNull("似乎没有任何数据");
        } else {
            inform.success("信息加载成功", smogInfs);
        }

        return inform;
    }

    /*
   ============================================================================
                                 heater
   ============================================================================
    */
    @GetMapping(value = "getHeaterLimit")
    public ManageInf getHeaterLimit(@RequestParam("limit") int limit,
                                    @RequestParam("page") int page,
                                    @RequestParam("name") String name,
                                    @RequestParam("room_id") String room_id,
                                    @RequestParam("home_id") String home_id) {
        ManageInf inform = new ManageInf();

        int index = (page - 1) * limit;

        List<EquipmentInf> smogInfs;

        smogInfs = heaterMapper.getHeaterLimit(name, room_id, home_id, index, limit);
        int count = heaterMapper.getHeaterLimitCount(name, room_id, home_id);

        inform.setCount(count);
        if (smogInfs.isEmpty()) {
            inform.isNull("似乎没有任何数据");
        } else {
            inform.success("信息加载成功", smogInfs);
        }

        return inform;
    }

}

